package com.sunyard.inser.web.controller;

import java.io.InputStream;
import java.nio.charset.Charset;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sunyard.inser.dto.DataTablesResult;
import com.sunyard.inser.dto.Result;
import com.sunyard.inser.sys.entity.UserInfo;
import com.sunyard.inser.web.util.TokenUtil;


@RestController
@RequestMapping("/sys")
public class SysController {


	@PostMapping("/login")
	public Result<String> login(@RequestBody UserInfo user) {
		Result<String> result = new Result<String>();
		result.setCode("0000");
 
		UserInfo userInfo = new UserInfo();
		userInfo.setCode(user.getCode());
		userInfo.setName(user.getPasswd());
		result.setResult(TokenUtil.generateToken(userInfo));
		return result;
	}
	
	@GetMapping("/menu/nav")
	public String menuNav(@RequestHeader("token") String token) {
		System.out.println(token);
		System.out.println(Charset.defaultCharset());
        InputStream in = SysController.class.getClassLoader().getResourceAsStream("menuList.json") ;
        byte[] buf = new byte[102400];
        try {
            in.read(buf);
            String test = new String(buf, "UTF-8").trim();
            System.out.println(test);
            return test;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "500";
        }
	}
	
	@GetMapping("/user/info")
	public String userInfo(@RequestHeader("token") String token) {
		System.out.println(token);
		UserInfo userInfo = TokenUtil.parseToken(token);
		System.out.println(userInfo);
        InputStream in = SysController.class.getClassLoader().getResourceAsStream("userInfo.json") ;
        byte[] buf = new byte[102400];
        try {
            in.read(buf);
            String test = new String(buf, "UTF-8").trim();
            System.out.println(test);
            return test;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "500";
        }
	}
	
	/**
	 * 退出
	 */
	@PostMapping("/logout")
	public Result<String> logout() {
		Result<String> result = new Result<String>();
		return result;
	}
}
